package goodenglish.domain.model.lexicon;

import goodenglish.domain.model.LazyList;
import goodenglish.infrastructure.Config;
import goodenglish.infrastructure.FileUtil;
import goodenglish.infrastructure.repository.db.LexiconRepositoryDb;
import goodenglish.interfaces.BeanFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 词库
 * 
 * @author hp
 * 
 */
public class Lexicon {
	private String name;
	private String id;
	// 字幕不重复单词
	private List<NewWord> newWords;
	private List<StudyPlan> studyPlans;

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public void setNewWords(List<NewWord> newWords) {
		this.newWords = newWords;
	}

	public List<NewWord> getNewWords() {
		if (newWords instanceof LazyList) {
			lexiconRepository.loadNewWords(this);
		}
		return newWords;

	}

	public List<String> getSpells() {
		List<String> words = new ArrayList<String>();
		for (NewWord newWord : this.getNewWords()) {
			words.add(newWord.getSpell());
		}
		return words;
	}

	public List<NewWord> getNewWords(List<String> spells) {
		List<NewWord> newWords = new ArrayList<NewWord>();
		for (String spell : spells) {
			newWords.add(getWord(spell));
		}
		return newWords;
	}

	public List<NewWord> getNotPlanedNewWords(int size) {
		List<NewWord> notPlanedNewWords = new ArrayList<NewWord>();
		int addNum = 0;
		for (NewWord newWord : this.getNewWords()) {
			if (!newWord.isPlaned()) {
				notPlanedNewWords.add(newWord);
				addNum++;
				if (addNum == size) {
					break;
				}
			}
		}
		return notPlanedNewWords;

	}

	/**
	 * 新增加单词，会做重复判断
	 * 
	 * @param newWord
	 */
	public void addNewWord(NewWord newWord) {
		if (this.newWords == null) {
			newWords = new ArrayList<NewWord>();
		}
		if (getWord(newWord.getSpell()) == null) {
			newWords.add(newWord);
		}

	}

	public NewWord getWord(String wordSpell) {
		for (NewWord word : getNewWords()) {
			if (word.getSpell().equals(wordSpell)) {
				return word;
			}
		}
		return null;
	}

	@Override
	public String toString() {
		return this.getName();
	}

	public void addStudyPlan(StudyPlan studyPlan) {
		if (null == studyPlans) {
			studyPlans = new ArrayList<StudyPlan>();
		}
		studyPlans.add(studyPlan);

	}

	public void setStudyPlans(List<StudyPlan> studyPlans) {
		this.studyPlans = studyPlans;
	}

	public List<StudyPlan> getStudyPlans() {
		if (studyPlans instanceof LazyList) {
			lexiconRepository.loadStudyPlans(this);
		}
		return studyPlans;
	}

	/**
	 * 导出单词例句到文件
	 */
	public void exportNewSubtSentesToFile() {
		StringBuffer stringBuffer = new StringBuffer();
		Set<String> set = new HashSet<String>();
		File exportSentensPath = new File(FileUtil.getHomeDirectory().getAbsolutePath() + "/句子.txt");
		for (NewWord subtWord : this.getNewWords()) {
			stringBuffer.append(subtWord.getExampleSentStr());
			stringBuffer.append(Config.LINE_SPLITER);
		}

		FileUtil.writeStrToTxtFileReplaceIfExsit(exportSentensPath, stringBuffer.toString());
	}

	/**
	 * 删除单词
	 * 
	 * @param wordSpell
	 */
	public void removeWord(String wordSpell) {
		for (NewWord word : getNewWords()) {
			if (word.getSpell().equals(wordSpell)) {
				getNewWords().remove(word);
				return;
			}
		}
	}

	public void editNewWordExplain(String spell, String explain) {
		spell = spell.trim();
		NewWord newWord = getWord(spell);
		if (newWord != null) {
			newWord.setExplain(explain);
		}
	}

	private LexiconRepository lexiconRepository = BeanFactory.getSingleInstance(LexiconRepositoryDb.class);

}
